<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/19
 * Time: 下午8:12
 */

class ModSpecialChannel extends Model {

    protected $table = 'special_channel';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbOss;
    }

    /**
     * 获取专服id,专服名称、渠道id，得以获取渠道所对应的专服名
     * @return unknown
     */
    public function getSpecialChannel(){
        $sql = 'SELECT sc.special_id,sc.channel_id,ss.name FROM special_channel as sc INNER JOIN special_server as ss on ss.id = sc.special_id;';
        $result = $this->getDbConnection()->getAll($sql, $args);
        return $result;
    }
    
    /**
     * 根据专服id获取该专服下的渠道
     */
    public function channelForSpecialId($special_id){
    	$sql = "SELECT sc.special_id,c.id,c.channel_name from special_channel as sc INNER JOIN channel as c on c.id = sc.channel_id WHERE sc.special_id = {$special_id};";
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result;
    }
    
    /**
     * 根据专服id获取该专服下的服务器
     */
    public function serverForSpecialId($special_id){
    	$sql = "SELECT ss.special_id,ss.server_id,s.server_name,s.server_num FROM special_servicer as ss INNER JOIN server as s ON s.id = ss.server_id where special_id = {$special_id};";
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result;
    }
    
    public function addSpecialChannel($params){
    	$result = $this->getDbConnection()->multiInsert($params, $this->table, FALSE);
    	return $result;
    }
    
    /**
     * 根据渠道获取专服s
     * @param unknown $id
     */
    public function getSpecial($id){
    	$sql = "SELECT ss.id,ss.name,sc.channel_id FROM special_channel as sc INNER JOIN special_server as ss ON ss.id = sc.special_id  WHERE sc.channel_id ={$id}";
    	 
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result; 
    }
    
    
    /**
     * 获取所有专服-渠道之间关系的数据
     */
    public function channelForSpecialIdAll($game){
    	$sql = "SELECT ss.name as special_name,sc.special_id,c.id as channel_id,c.channel_name,c.tab from special_channel as sc INNER JOIN channel as c on c.id = sc.channel_id ";
        $sql .= "INNER JOIN special_server as ss on ss.id = sc.special_id WHERE ss.game = '{$game}'";
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result;
    }
    
    /**
     * 获取所有专服-服务器之间关系的数据
     */
    public function serverForSpecialIdAll($game){
    	$sql = "SELECT sse.name as special_name,ss.special_id,ss.server_id,s.server_name,s.server_num FROM special_servicer as ss INNER JOIN server as s ON s.id = ss.server_id "; 
        $sql .= "INNER JOIN special_server as sse on sse.id = ss.special_id WHERE sse.game = '{$game}'";
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result;
    }
} 